<template>
  <div class="ele-body ele-body-ex ele-body-layout">
    <el-card shadow="never">
      <div slot="header" class="clearfix">
        <span>采集数据详情(默认显示今天数据)</span>
      </div>
      <!-- 表单搜索 -->
      <el-form
        :model="where"
        class="ele-form-search"
        label-width="75px"
        @keyup.enter.native="reload"
        @submit.native.prevent
      >
        <div class="app-container">
          <div class="filter-container">
            <el-select
              v-model="where.bazaarId"
              class="filter-item"
              clearable
              placeholder="推广行业"
              style="width: 180px"
              @change="reload"
            >
              <el-option
                v-for="item in BazaarList"
                :key="item.id"
                :disabled="item.status === 0"
                :label="item.name"
                :value="item.id"
              />
            </el-select>
            <el-select
              v-model="where.userStat"
              class="filter-item"
              clearable
              placeholder="笔记类别"
              style="width: 160px"
              @change="reload"
              @clear="reload"
            >
              <el-option
                v-for="item in userStatList"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
            <el-input
              v-model="where.company"
              class="filter-item"
              clearable
              placeholder="所属公司"
              style="width: 180px"
              @clear="reload"
            />
            <el-input
              v-model="where.keywords"
              class="filter-item"
              clearable
              placeholder="关键词"
              style="width: 180px"
              @clear="reload"
            />
            
            <el-input
              v-model="where.title"
              class="filter-item"
              clearable
              placeholder="笔记标题"
              style="width: 180px"
              @clear="reload"
            />
        
            <el-date-picker
              v-model="daterange"
              :clearable="false"
              :picker-options="pickerOptions"
              end-placeholder="数据结束日期"
              range-separator="-"
              start-placeholder="数据开始日期"
              style="width: 230px; margin-right: 6px"
              type="datetimerange"
              unlink-panels
              value-format="yyyy-MM-dd HH:mm:ss"
              @change="onDateRangeChoose"
            />

            <el-button
              class="filter-item"
              icon="el-icon-search"
              type="primary"
              @click="reload"
              >搜索
            </el-button>
            <el-button
              class="filter-item"
              icon="el-icon-refresh"
              style="margin-left: 2px"
              @click="reset"
              >重置
            </el-button>
          </div>
        </div>
      </el-form>

      <!-- 数据表格 -->
      <ele-pro-table
        ref="table"
        :columns="columns"
        :datasource="url"
        :max-height="defaultHeight"
        :parse-data="parseData"
        :selection.sync="selection"
        :where="where"
      >
        <!-- 推广行业列 -->
        <template slot="bazaarId" slot-scope="{ row }">
          <template v-for="item in BazaarList">
            <span v-if="row.bazaarId === item.id" :key="item.id">
              {{ item.name }}
            </span>
          </template>
        </template>

        <!-- 笔记类别列 -->
        <template slot="type" slot-scope="{ row }">
          <el-tag size="mini" v-if="row.type == 1">广告</el-tag>
          <el-tag type="success" v-else size="mini">赞助</el-tag>
        </template>

        <!-- 落地页列 -->
        <template slot="url" slot-scope="{ row }">
          <el-link :href="row.url" target="_blank" type="primary"
            ><i class="el-icon-_globe"></i> 访问URL</el-link
          >
        </template>

        <!-- 落地页列 -->
        <template slot="title" slot-scope="{ row }">
          <el-link :href="row.url" target="_blank">{{ row.title }}</el-link>
        </template>

        <!-- 表头工具栏 -->
        <template slot="toolbar">
          <el-button
            icon="el-icon-delete"
            plain
            size="small"
            type="danger"
            @click="openDelEdit({})"
            >删除无关笔记
          </el-button>
        </template>
      </ele-pro-table>
    </el-card>

    <!-- 删除弹窗 -->
    <del-edit :data="current" :visible.sync="showDelEdit" @done="reload" />
  </div>
</template>

<script>
import DelEdit from "./del-edit";
export default {
  name: "xhsDataurl",
  components: { DelEdit },
  data() {
    return {
      //市场列表
      BazaarList: [],
      // 表格高度
      defaultHeight: 0,
      // 推广账户状态
      userStatList: [
        { value: 1, label: "广告" },
        { value: 2, label: "赞助" },
      ],
      // 表格数据接口
      url: "/Xhsdataurl/read",
      // 筛选日期范围
      daterange: this.getDaterange(),
      // 日期时间选择器快捷项
      pickerOptions: {
        shortcuts: [
          {
            text: "今天",
            onClick(picker) {
              let start = new Date();
              start.setTime(start.getTime());
              start =
                start.getFullYear() +
                "-" +
                (start.getMonth() + 1) +
                "-" +
                start.getDate() +
                " 00:00:00";
              let end = new Date();
              end.setTime(end.getTime() + 24 * 60 * 60 * 1000);
              end =
                end.getFullYear() +
                "-" +
                (end.getMonth() + 1) +
                "-" +
                end.getDate() +
                " 00:00:00";
              picker.$emit("pick", [start, end]);
            },
          },
          {
            text: "昨天",
            onClick(picker) {
              let start = new Date();
              start.setTime(start.getTime() - 24 * 60 * 60 * 1000);
              start =
                start.getFullYear() +
                "-" +
                (start.getMonth() + 1) +
                "-" +
                start.getDate() +
                " 00:00:00";
              let end = new Date();
              end.setTime(end.getTime());
              end =
                end.getFullYear() +
                "-" +
                (end.getMonth() + 1) +
                "-" +
                end.getDate() +
                " 00:00:00";
              picker.$emit("pick", [start, end]);
            },
          },
          {
            text: "最近一周",
            onClick(picker) {
              let start = new Date();
              start.setTime(start.getTime() - 7 * (24 * 60 * 60 * 1000));
              start =
                start.getFullYear() +
                "-" +
                (start.getMonth() + 1) +
                "-" +
                start.getDate() +
                " 00:00:00";
              let end = new Date();
              end.setTime(end.getTime());
              end =
                end.getFullYear() +
                "-" +
                (end.getMonth() + 1) +
                "-" +
                end.getDate() +
                " 00:00:00";
              picker.$emit("pick", [start, end]);
            },
          },
          {
            text: "最近半月",
            onClick(picker) {
              let start = new Date();
              start.setTime(start.getTime() - 15 * (24 * 60 * 60 * 1000));
              start =
                start.getFullYear() +
                "-" +
                (start.getMonth() + 1) +
                "-" +
                start.getDate() +
                " 00:00:00";
              let end = new Date();
              end.setTime(end.getTime());
              end =
                end.getFullYear() +
                "-" +
                (end.getMonth() + 1) +
                "-" +
                end.getDate() +
                " 00:00:00";
              picker.$emit("pick", [start, end]);
            },
          },
          {
            text: "最近一个月",
            onClick(picker) {
              let start = new Date();
              start.setTime(start.getTime() - 30 * (24 * 60 * 60 * 1000));
              start =
                start.getFullYear() +
                "-" +
                (start.getMonth() + 1) +
                "-" +
                start.getDate() +
                " 00:00:00";
              let end = new Date();
              end.setTime(end.getTime());
              end =
                end.getFullYear() +
                "-" +
                (end.getMonth() + 1) +
                "-" +
                end.getDate() +
                " 00:00:00";
              picker.$emit("pick", [start, end]);
            },
          },
          {
            text: "最近三个月",
            onClick(picker) {
              let start = new Date();
              start.setTime(start.getTime() - 90 * (24 * 60 * 60 * 1000));
              start =
                start.getFullYear() +
                "-" +
                (start.getMonth() + 1) +
                "-" +
                start.getDate() +
                " 00:00:00";
              let end = new Date();
              end.setTime(end.getTime());
              end =
                end.getFullYear() +
                "-" +
                (end.getMonth() + 1) +
                "-" +
                end.getDate() +
                " 00:00:00";
              picker.$emit("pick", [start, end]);
            },
          },
          {
            text: "最近半年",
            onClick(picker) {
              let start = new Date();
              start.setTime(start.getTime() - 180 * (24 * 60 * 60 * 1000));
              start =
                start.getFullYear() +
                "-" +
                (start.getMonth() + 1) +
                "-" +
                start.getDate() +
                " 00:00:00";
              let end = new Date();
              end.setTime(end.getTime());
              end =
                end.getFullYear() +
                "-" +
                (end.getMonth() + 1) +
                "-" +
                end.getDate() +
                " 00:00:00";
              picker.$emit("pick", [start, end]);
            },
          },
          {
            text: "最近一年",
            onClick(picker) {
              let start = new Date();
              start.setTime(start.getTime() - 365 * (24 * 60 * 60 * 1000));
              start =
                start.getFullYear() +
                "-" +
                (start.getMonth() + 1) +
                "-" +
                start.getDate() +
                " 00:00:00";
              let end = new Date();
              end.setTime(end.getTime());
              end =
                end.getFullYear() +
                "-" +
                (end.getMonth() + 1) +
                "-" +
                end.getDate() +
                " 00:00:00";
              picker.$emit("pick", [start, end]);
            },
          },
        ],
      },

      // 表格列配置
      columns: [
        {
          columnKey: "index",
          type: "index",
          width: 80,
          label: "序号",
          align: "center",
          showOverflowTooltip: true,
        },
        {
          prop: "bazaarId",
          slot: "bazaarId",
          label: "所属行业",
          sortable: "custom",
          showOverflowTooltip: true,
        },
        {
          prop: "keywords",
          label: "关键词",
          sortable: "custom",
          showOverflowTooltip: true,
        },
        {
          prop: "type",
          slot: "type",
          label: "笔记类别",
          sortable: "custom",
          showOverflowTooltip: true,
        },
        {
          prop: "title",
          slot: "title",
          label: "笔记标题",
          sortable: "custom",
          showOverflowTooltip: true,
        },
        {
          prop: "company",
          label: "所属公司",
          sortable: "custom",
          showOverflowTooltip: true,
        },
        {
          prop: "locationTV",
          label: "地理位置",
          sortable: "custom",
          showOverflowTooltip: true,
        },
        {
          prop: "comments",
          label: "评论数",
          sortable: "custom",
          showOverflowTooltip: true,
        },
        {
          prop: "love",
          label: "点赞数",
          sortable: "custom",
          showOverflowTooltip: true,
        },
        {
          prop: "star",
          label: "收藏数",
          sortable: "custom",
          showOverflowTooltip: true,
        },
        {
          prop: "showpos",
          label: "展示位置",
          sortable: "custom",
          showOverflowTooltip: true,
        },
        {
          prop: "number",
          label: "展示次数",
          sortable: "custom",
          showOverflowTooltip: true,
        },
        {
          prop: "datetime",
          label: "笔记发布时间",
          sortable: "custom",
          showOverflowTooltip: true,
        },
        {
          prop: "url",
          slot: "url",
          label: "URL",
          sortable: "custom",
          showOverflowTooltip: true,
        },
        {
          width: 140,
          prop: "time",
          label: "第一次采集时间",
          sortable: "custom",
          showOverflowTooltip: true,
        },
        {
          width: 140,
          prop: "lasttime",
          label: "最后采集时间",
          sortable: "custom",
          showOverflowTooltip: true,
        },
      ],
      // 表格搜索条件
      where: {},
      // 是否显示编辑弹窗
      showEdit: false,
      // 表格选中数据
      selection: [],
      // 当前编辑数据
      current: null,
      // 是否显示删除弹窗
      showDelEdit: false,
    };
  },
  created() {
    window.addEventListener("resize", this.getHeight);
    this.getHeight();
  },

  methods: {
    /*设置表格高度*/
    getHeight() {
      this.defaultHeight = window.innerHeight - 360;
    },

    /* 显示编辑 */
    openDelEdit(row) {
      this.current = Object.assign({}, this.where, row);
      this.showDelEdit = true;
    },

    /*表格数据加载*/
    parseData(res) {
      if (res.code === 0) {
        this.BazaarList = res.bazaar;
      }
      return {
        code: res.code,
        data: res.data,
        count: res.count,
        msg: res.msg,
      };
    },

    /* 刷新表格 */
    reload(isPage) {
      let obj = { where: this.where };
      if (!isPage) {
        obj.page = 1;
      }
      this.$refs.table.reload(obj);
    },

    /* 重置搜索 */
    reset() {
      for (let key in this.where) {
        this.where[key] = null;
      }
      this.reload();
    },

    getDaterange() {
      let start = new Date();
      start.setTime(start.getTime());
      start =
        start.getFullYear() +
        "-" +
        (start.getMonth() + 1) +
        "-" +
        start.getDate() +
        " 00:00:00";
      let end = new Date();
      end.setTime(end.getTime() + 24 * 60 * 60 * 1000);
      end =
        end.getFullYear() +
        "-" +
        (end.getMonth() + 1) +
        "-" +
        end.getDate() +
        " 00:00:00";
      return [start, end];
    },

    /* 日期选择改变回调 */
    onDateRangeChoose() {
      if (this.daterange && this.daterange.length === 2) {
        this.where.createTimeStart = this.daterange[0];
        this.where.createTimeEnd = this.daterange[1];
      } else {
        this.where.createTimeStart = null;
        this.where.createTimeEnd = null;
      }
    },
  },
};
</script>

<style scoped>
.ele-table-tool .is-disabled {
  cursor: not-allowed;
  filter: Alpha(opacity=0.65);
  -moz-opacity: 0.65;
  opacity: 0.65;
}
</style>
